package rcs34.android.photopublishare;

import java.io.File;
import java.io.IOException;
import java.util.GregorianCalendar;

import rcs34.android.libs.ExifDriver.ExifDriver;
import rcs34.android.libs.ExifDriver.ExifManager;

import Utils.ImageUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class ActivityProfile extends Activity {
	private final String LOGTAG = getClass().getName();
	private ProgressDialog progressDialog;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		progressDialog = ProgressDialog.show(this, "Saving profile", "");
		setContentView(R.layout.activity_profile);
		Intent intent = this.getIntent();
		ProfileSetter setter = new ProfileSetter();
		setter.execute(GApp.exifDriver);
	}

	private class ProfileSetter extends AsyncTask<ExifDriver, String, Integer> {
		@Override
		protected Integer doInBackground(ExifDriver... _driver) {
			if (_driver[0] != null) {
				Log.v(LOGTAG,"Setting profile"); 
				ExifManager exif = new ExifManager(_driver[0], GApp.context);
				exif.setArtist("Name of artist");
				exif.setMarkerNote(new String("Marker note - here time in milis"+new GregorianCalendar().getTimeInMillis()+"").getBytes());
				exif.setCopyright("Photographer's copyright", "Editor's copyright");
				exif.setImageDescription("Simple image description");
				exif.setUserComment("User comment can contain wide characters křížala kůrčičku má hořkou");
				File cacheDir = GApp.context.getCacheDir();
				File tempFile=null;
				try {
					tempFile = File.createTempFile("publishare_exif_save",
							null, cacheDir);
					_driver[0].save(tempFile.getAbsolutePath());
					if (tempFile.exists()) {
						ImageUtils.copyFile(tempFile,
								new File(_driver[0].getSourceFile()));
						tempFile.delete();
					}
				} catch (IOException e) { 
					if(tempFile!=null && tempFile.exists()){tempFile.delete();};
					e.printStackTrace();  
				}finally{
					if(tempFile!=null && tempFile.exists()){tempFile.delete();};
				}

			}
			return 0; 
		}

		@Override
		protected void onPostExecute(Integer _dummy) {
			progressDialog.dismiss();
			Intent result = new Intent();
			setResult(Activity.RESULT_OK, result);
			finish();
		}
	}
}
